package com.bilu.common.utility;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletRequest;


/**
 * 请求对象工具类
 */
public class RequestUtility {
	/**
	 * 从请求对象中以字符流的形式读取请求参数的字符串形式<br/>
	 * 编码方式为UTF-8格式<br/>
	 * 如果读取失败则返回空（null）<br/>
	 */
	public static String readRequest(HttpServletRequest request){
 		StringBuilder buffer = new StringBuilder();
 		BufferedReader reader = null;
 		try {
 			reader = new BufferedReader(new InputStreamReader(request.getInputStream(),StandardCharsets.UTF_8));
 			String line = reader.readLine();
 			while (line != null) {
 				buffer.append(line);
 				line = reader.readLine();
 			}
			return buffer.toString();
 		} catch (Exception e) {
 			return null;
 		} finally {
			StreamUtility.closeStream(reader);
 		}
 	}
}
